import wx  
import wx.lib.sized_controls as sc  
import time  
import datetime  
import logging
import binascii

class Dialog(sc.SizedDialog): 
    logger = logging.getLogger("rollo.pololu.TReX")
     
    def __init__(self, parent, id, trex):  
        sc.SizedDialog.__init__(self, None, -1, "Rollo", style=wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER)            
	self.trex=trex
        pane = self.GetContentsPane()  
        pane.SetSizerType("form")  

        grid = wx.GridSizer(3, 3)

        grid.AddMany([
            (wx.StaticText(pane,-1,""), 0, wx.TOP | wx.LEFT, 9),
            (wx.Button(pane, wx.ID_UP), 0, wx.TOP, 9),
            (wx.StaticText(pane,-1,""), 0, wx.TOP | wx.RIGHT, 9),
            (wx.Button(pane, wx.ID_BACKWARD), 0, wx.LEFT, 9),
	    (wx.Button(pane, wx.ID_REFRESH)),
            (wx.Button(pane, wx.ID_FORWARD), 0, wx.RIGHT, 9),
            (wx.StaticText(pane,-1,""), 0, wx.BOTTOM | wx.LEFT, 9),
            (wx.Button(pane, wx.ID_DOWN), 0, wx.BOTTOM, 9),
            (wx.Button(pane, wx.ID_STOP), 0, wx.BOTTOM | wx.RIGHT, 9)
	    ])

	pane.SetSizer(grid)
        self.Bind(wx.EVT_BUTTON, self.forwards, id=wx.ID_UP)
        self.Bind(wx.EVT_BUTTON, self.backwards, id=wx.ID_DOWN)
        self.Bind(wx.EVT_BUTTON, self.left, id=wx.ID_BACKWARD)
        self.Bind(wx.EVT_BUTTON, self.right, id=wx.ID_FORWARD)
        self.Bind(wx.EVT_BUTTON, self.stop, id=wx.ID_STOP)
        self.Bind(wx.EVT_BUTTON, self.refresh, id=wx.ID_REFRESH)
	self.Bind(wx.EVT_LEFT_UP,self.stop)

#        wx.StaticText(pane, -1, "T2:")
 #       self.T2_BOX = wx.SpinCtrl(pane, -1, "", (40,40), (60,-1))
  #      self.T2_BOX.SetRange(1,60)
   #     self.T2_BOX.SetValue(5)

        RUN_BUTTON = self.SetButtonSizer(self.CreateStdDialogButtonSizer(wx.OK))

        self.Fit()
        self.SetMinSize(self.GetSize())

    def left(self,event):
	self.trex.left()

    def right(self,event):
	self.trex.right()

    def forwards(self,event):
	self.trex.forwards()

    def backwards(self,event):
	self.trex.backwards()

    def stop(self,event):
	self.trex.stop()

    def right(self,event):
	self.trex.right()

    def log_resp(self, message, resp):
	y=''
	if resp != None:
		y=binascii.hexlify(resp)
	self.logger.debug("{0}: '{1}' (hex '{2}')".format(message,resp,y))

    def refresh(self,event):
#	self.log_resp("sig",self.trex.get_sig())
#	self.log_resp("raw_channels",self.trex.get_raw_channels())
	self.log_resp("currents",self.trex.get_currents())

